<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/calendar.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/calendar.css" type="text/css" />
<title>Insert title here</title>
<style type="text/css">
	td{
		height: 60px;
		width: 180px;
		text-align: center;
	}
</style>
</head>
<body>
<div align="center">
	<form name="f">
	<input type="hidden" name="shopId" value="${sessionScope.shop.shopId }">
		<p>
			<label for="birthday">起始日期：</label>
			<input type="text" id="birthday" name="beginDay" value="1990-01-01"/>
			<!-- <label for="birthday">结束日期：</label> -->
			<input type="hidden" id="birthday" name="endDay" value="2018-07-01"/>
			<input type="button" value="查询 " onclick="queryOrder()"/>
		</p>
	</form>
</div>
<hr>
<div id="showTable" align="center" style="margin-top: 60px;"></div>
</body>
<script type="text/javascript">
	queryOrder();
	function queryOrder(){
		var shopId = f.shopId.value;
		var beginDay = f.beginDay.value;
		var endDay = f.endDay.value;
		$.ajax({
			type:"get",//设置提交方式
			url:"${pageContext.request.contextPath}/queryOrder",
			async:true,//表示进行异步执行
			dataType:"json",//数据的返回类型
			data:{
				shopId:shopId,
				beginDay:beginDay,
				endDay:endDay
				},
			success:function(data){//data就是成功之后 Servlet返回的结果
				var state = ""; 
				var str = "<table><tr><th>商品</th><th>购买人</th><th>购买人电话</th><th>购买时间</th><th>购买数量</th><th>收货地址</th><th>商品总额</th><th>订单状态</th><th>交易操作</th></tr>";
				for(var i = 0;i<data.length;i++){
					if(data[i].orderState == 0){
						state = "待付款";
					}else if(data[i].orderState == 1){
						state = "待发货";
					}else if(data[i].orderState == 2){
						state = "待收货";
					}else{
						state = "待评价";
					}
					str += "<tr><td>"+data[i].goodsName+"</td><td>"+data[i].userName+"</td><td>"+data[i].userTel+"</td><td>"+data[i].time+"</td><td>"+data[i].orderNum+"</td><td>"+data[i].orderAddress+"</td><td>"+data[i].orderNum*data[i].goodsPrice+"</td><td>"+state+"</td><td><a href='#' onclick='fahuo("+data[i].orderId+","+data[i].orderState+")'>发货</td></tr>"
				}
				str += "</table>"; 
				$("#showTable").html(str);
			},
			error:function(err){
				alert("操作失败");
			}
		});
	}
	function fahuo(orderId,orderState){
		if(orderState == 0){
			alert("买家未付款");
		}else if(orderState == 1){
			$.ajax({
				type:"post",//设置提交方式
				url:"${pageContext.request.contextPath}/fahuo",
				async:true,//表示进行异步执行
				dataType:"json",//数据的返回类型
				data:{
					_method:"PUT",
					orderId:orderId
					},
				success:function(data){//data就是成功之后 Servlet返回的结果
					if(data[0] == 1){
						alert("发货成功");
						queryOrder();
					}else{
						alert("发货失败");
					}
				},
				error:function(err){
					alert("操作失败");
				}
			});
		}else{
			alert("你已经发货");
		}
	}
</script>
</html>